<!--
*@Author: QWP
*@Description: 公文编辑
*@Date: 2024-07-08 17:04:38
-->
<template>
  <u-dialog v-bind="$attrs"
    destroyOnClose
    :confirmLoading="loading"
    :hideBtns="isCheck ? ['submit'] : []"
    @close="dialogClose"
    @submit="submit">
    <u-form :elements="formEls"
      :model="form"
      :gutter="0"
      :loading="loading"
      :disabled="isCheck"
      ref="formRef">
    </u-form>
  </u-dialog>
</template>

<script lang='ts' setup>
import { ref, reactive, computed, onMounted, h, watch, useAttrs } from 'vue' 
import { useRoute, useRouter } from 'vue-router'
import { useBaseStore, useLoginStore } from '@/store'
import { storeToRefs } from 'pinia'
import { _IFormEl } from '@/src/components/common/form/interface';
import { FormInstance, FormItemProps } from 'ant-design-vue';
import { HomeOutlined, FileTextOutlined, UploadOutlined } from '@ant-design/icons-vue';
import SignAudit from './SignAudit.vue'
import UIconFont from '@/components/common/iconfont/IconFont.vue'
import { isSel1 } from '@/common/model/base'
import { nullC, alertC, messageC } from '@/util/common'
import { useAuditAbout, IAuditParams } from '@/hooks/useAudit'
import * as studyApi from '@/service/assist/sudyJudge'
import { cloneDeep } from 'lodash'
import { types } from './data.ts'
import dayjs from 'dayjs';

const props = defineProps<{
  current?: object,
}>()

const attrs = useAttrs()
const route = useRoute()
const { INC_NODE, EMERGENCY_DEGREE, OPEN_FORM, fileNoChars } = storeToRefs(useBaseStore())
const { userInfo } = storeToRefs(useLoginStore())

const emits = defineEmits(['success'])

// 发布后则不能编辑
const isCheck = computed(() => form.value.isrelease == 1)

const loading = ref<boolean>(false)
const formRef = ref<FormInstance>()
const form = ref({})
const formItemProps: FormItemProps = {
  labelWidth: '140px',
  colon: false
}
const formEls: Array<_IFormEl> = [
  { label: '类型', key: 'type', col: { span: 24 }, element: 'select', 
    formItemProps: { ...formItemProps, required: true } ,
    componentProps: { 
      options: types
    }
  },
  { label: '标题', key: 'content', col: { span: 24 }, element: 'input', 
    formItemProps: { ...formItemProps, required: true } ,
    componentProps: {
      placeholder: '默认自动取研判文件名称'
    }
  },
  { label: '研判文件', key: 'appendixSaveVOList', col: { span: 24 }, element: 'file',
    formItemProps: { ...formItemProps, required: true },
    componentProps: {
      // accept: '.doc, .docx',
      fileParams: { biztype: '3', bucket: 'DOC_BIZ_TYPE_JUDGMENT', maxCount: 1}
    } 
  },
  // { label: '备注', key: 'contents', col: { span: 24 }, element: 'textarea', 
  //   formItemProps: { ...formItemProps, }, 
  // },
]

// 保存提交
const submit = async(type?: string) => {
  try {
    await formRef.value.form.validate()
  } catch (error) {
    alertC("请先表单信息填写完毕!");
    return
  }
  
  const url = form.value.keyid ? 'update' : 'add';

  await alertC("确认保存？", 'confirm')
  
  loading.value = true;
  try {
    const res = await studyApi[url](form.value)
    if(res.code == 0) {
      if(url == 'add') {
        form.value.keyid = res.data['keyid']
        form.value.status = res.data["status"];
      }
     
      dialogClose()
      emits('success')
    }
    else {
      messageC(res.msg, "error")
    }
  } catch (error) { }
  loading.value = false;
}

// 新增初始化元数据
const initFormInfo = () => {
  form.value = {
    fkunitsid: userInfo.value.unitId,
    fkdeptid: userInfo.value.deptId,
    redeptname: userInfo.value.deptName,
    username: userInfo.value.name,
  }
}

// 获取回显信息
const echoInfo = async () => {
  loading.value = true;
  try {
    const res = await studyApi.query(props.current['keyid']);
    if(res.code == 0) {
      let data = res.data;
      form.value = res.data;
      form.value.type = form.value.type + '';

      form.value.appendixSaveVOList = form.value['appendix'] || [];
    }
  } catch (error) { }
  loading.value = false;
}

// 弹窗关闭
const dialogClose = () => {
  formRef.value.form.resetFields();
  form.value = {}
}

watch(()  => attrs.open, (nval, oval) => {
  if(nval == false) return

  if(props.current['keyid']) {
    echoInfo()
  }
  else {
    initFormInfo()
  }
})

watch(() => form.value.appendixSaveVOList, (nval, oval) => {
  if((nval || []).length == 0) return 
  let fileName = (nval[0]['filename'] || '').split('.')//.pop()
  fileName.pop()
  form.value.content = fileName.join('')
  
  formRef.value.form.clearValidate(['appendixSaveVOList'])
}, { deep: true })
</script>

<style lang='scss' >
</style>